package cas.ihep.hep.util;

import cas.ihep.fs.UnifiedBlock;
import cas.ihep.fs.UnifiedFile;
import cas.ihep.fs.UnifiedFileSystem;
import cas.ihep.util.MemoryBuffer;
import com.google.common.io.Closer;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.net.URI;

public class NativeBlock implements Closeable{
    private Closer closer;
    private long addr;
    private MemoryBuffer membuffer;
    NativeBlock(URI path)throws IOException{
        closer=Closer.create();
        UnifiedFile file=closer.register(UfsHolder.ufs.open(path));
        UnifiedBlock blk=file.block(0);
        membuffer=closer.register(new MemoryBuffer(new File(blk.absolutePath())));
        addr=membuffer.address();
    }
    public long address(){
        return addr;
    }
    public void close()throws IOException{
        closer.close();
    }

    public MemoryBuffer getBuffer(){
        return membuffer;
    }

    private static class UfsHolder{
        private static final UnifiedFileSystem ufs=UnifiedFileSystem.get(UnifiedFileSystem.newConfiguration(System.getenv("HADOOP_HOME")));
    }
}
